package threading;

import java.util.Scanner;

/**
 * @author 徐志荣
 * @date 2023/3/16 20:41
 */
public class ThreadDemo14 {

    //3. 使用volatile 关键字修饰,
    volatile public static int flg = 0;

    public static void main(String[] args) {
       /*
       1. 由于编译器优化会产生bug
       Thread t1 = new Thread(()->{
           while (flg ==0){
               //空着
           }
            System.out.println("t1 结束");
        });*/

        // 2. 循环中不再是空着的,而是休眠一下
        //程序的bug就被 解决了
        //避免 编译器优化
      /*  Thread t1 = new Thread(()->{
            while (flg ==0){
                try {
                    Thread.sleep(10);
                } catch (InterruptedException e) {
                    e.printStackTrace();
                }
            }
            System.out.println("t1 结束");
        });*/


        //3.

        Thread t1 = new Thread(()->{
            while (flg ==0){
                //空着
            }
            System.out.println("t1 结束");
        });







        Thread t2 = new Thread(() ->{
            Scanner scanner = new Scanner(System.in);
            System.out.println("输入整数");
            flg = scanner.nextInt();
        });

        t1.start();
        t2.start();

        

    }
}
